#encoding:utf-8
from django.shortcuts import render, HttpResponseRedirect
from .models import Article, Category, BlogComment, Tag    #我们要先导入我们新增的这个表
from .forms import BlogCommentForm


def index(request):
    article_list = Article.objects.all()
    category_list = Category.objects.all().order_by('name')
    tag_list = Tag.objects.all().order_by('name')
    date_archive = Article.objects.archive()
    for tag in tag_list:
        tag.length = len(Article.objects.filter(tag__id=tag.id))
    return render(request, 'article/index.html', {'article_list': article_list, 'category_list': category_list,
                                                  'tag_list': tag_list, 'date_archive': date_archive})


def article_detail(request, article_id):
    if request.method == 'POST':
        form = BlogCommentForm(request.POST)
        if form.is_valid():
            comment_form = form.cleaned_data
            user_name = comment_form['user_name']
            user_email = comment_form['user_email']
            body = comment_form['body']
            comment = BlogComment.objects.create(
                user_name=user_name,
                user_email=user_email,
                body=body,
                article=Article.objects.get(pk=article_id))
            comment.save()
        return HttpResponseRedirect('/detail/%s/' % article_id)

    else:
        form = BlogCommentForm()
        article = Article.objects.get(pk=article_id)
        comment_list = BlogComment.objects.filter(article__id=article_id)
    return render(request, 'article/detail.html', {'article': article, 'comment_list': comment_list, 'form': form})


def category_show(request, category_id):
    article_list = Article.objects.filter(category__id=category_id)
    category_list = Category.objects.all().order_by('name')
    return render(request, 'article/index.html', {'article_list': article_list, 'category_list': category_list})


def about_me(request):
    return render(request, 'article/about_me.html')


def post_comment(request, article_id):
    if request.method == 'POST':
        form = BlogCommentForm(request.POST)
        if form.is_valid():
            comment_form = form.cleaned_data
            user_name = comment_form['user_name']
            user_email = comment_form['user_email']
            body = comment_form['body']
            comment = BlogComment.objects.create(
                user_name=user_name,
                user_email=user_email,
                body=body,
                article=Article.objects.get(pk=article_id))
            comment.save()
            return HttpResponseRedirect('/detail/%s/' % article_id)
    else:
        form = BlogCommentForm()
    return render(request, 'article/comment.html', {'form': form})


def tag_view(request, tag_id):
    article_list = Article.objects.filter(tag__id=tag_id)
    category_list = Category.objects.all().order_by('name')
    tag_list = Tag.objects.all().order_by('name')
    date_archive = Article.objects.archive()
    for tag in tag_list:
        tag.length = len(Article.objects.filter(tag__id=tag.id))
    return render(request, 'article/index.html', {'article_list': article_list, 'category_list': category_list,
                                                  'tag_list': tag_list, 'date_archive': date_archive})


def archive_view(request, year, month):
    article_list = Article.objects.filter(create_time__year=int(year), create_time__month=int(month))
    category_list = Category.objects.all().order_by('name')
    tag_list = Tag.objects.all().order_by('name')
    date_archive = Article.objects.archive()
    for tag in tag_list:
        tag.length = len(Article.objects.filter(tag__id=tag.id))
    return render(request, 'article/index.html', {'article_list': article_list, 'category_list': category_list,
                                                  'tag_list': tag_list, 'date_archive': date_archive})